<?php
namespace plugins\log\controller;
use cmf\controller\PluginAdminBaseController;
use think\Db;
use plugins\log\model\SysLogsConfigModel;


class ConfigController extends PluginAdminBaseController
{
	protected function _initialize()
	{
	}
	
	
	/**
	*	日志配置首页
	*/
	public function index()
	{
	    $param =$this->request->param();
	    $search = [
	        'keywords' => isset( $param [ 'keywords' ] ) ? $param [ 'keywords' ] : '',
	    ];
	    $list = SysLogsConfigModel::order( 'id' );
	    
	    if( !empty( $search [ 'keywords' ] )){
	        $list = $list->where( 'rule' , 'LIKE' , "%".$search['keywords']."%");
	    }
	    $list = $list->paginates(20);
	    
	    $this->assign( 'page' , $list->render( [10,20,50,100,500] ) );
	    
	    $logLevel = [ 
	        'H' => '高级',
	        'M' => '中级',
	        'OFF' => '关闭',
	    ];
	    
	    
	    try {
	        $level = file_get_contents( PLUGINS_PATH.'log'.DS.'config'.DS.'20200813.txt'  );
	        $levelStr = $logLevel[ $level ];
	        
	    } catch ( \Exception $e ) {
	        $levelStr = "关闭";
	        $level = "OFF";
	    }
	    
	    $this->assign( 'search' , $search );
	    $this->assign( 'levelStr' , $levelStr );
	    $this->assign( 'level' , $level );
	    
	    $this->assign( 'list' , $list );
	    return $this->fetch();
	}
	
	//日志级别
	public function logLevel()
	{
	    $data = $this->request->param();
	    $value = $data[ 'value' ];
	    try {
	        file_put_contents( PLUGINS_PATH.'log'.DS.'config'.DS.'20200813.txt' , $value );
	    } catch ( \Exception $e ) {
	        return api_json_echo( false , '修改级别失败'.$e->getMessage() );
	    }
	    
	    return api_json_echo( true , '修改成功' );
	    
	}
	
	public function add()
	{
	    return $this->fetch();
	}
	
	public function addPost()
	{
	    $param = $this->request->param();
	    $data = $param[ 'data' ];
	    $res = SysLogsConfigModel::create( $data );
	    if( !$res ){
	        return api_json_echo( false ,'保存失败' );
	    }
	    return api_json_echo( true ,'保存成功' );
	}
	
	public function edit($id)
	{
	    $conf = SysLogsConfigModel::get($id);
	    $this->assign( 'conf' , $conf );
	    return $this->fetch();
	}
	
	public function editPost()
	{
	    $param = $this->request->param();
	    $data = $param[ 'data' ];
	    $res = SysLogsConfigModel::update( $data );
	    if( !$res ){
	        return api_json_echo( false ,'保存失败' );
	    }
	    return api_json_echo( true ,'保存成功' );
	}
	
	
	public function dels( $id )
	{
	    if( !SysLogsConfigModel::where( 'id' , $id )->delete() ){
	        return api_json_echo( false ,'删除失败' );
	    }
	    return api_json_echo( true ,'删除成功' );
	}
	
	
	
	
}